import mdService from '@/api/MdService'
import cacheManage from '@/cache/CacheManage'
import ConstDef from '@/js/ConstDef'
import nvService from '@/api/NvService'
class MenuFunction {
  async getEntryConfigTabPage(mdCode, mvCode) {
    const commonInfo = cacheManage.getDefaultCommonInfo();
    const mvMsg = await mdService.getMv(commonInfo, mdCode, mvCode);
    const result = { success: false, message: null }
    if (!mvMsg.success) {
      result.message = mvMsg.message;
      return result;
    }
    const mv = mvMsg.value;
    if (mv.nvCode) { mv.action = 'query'; } else mv.action = 'init'
    if (mv.parentNvCode) {
      const ndCode = mv.ndCode;
      const codeFdName = mv.codeFdName;
      const idMsg = await nvService.getParentNvIdCodeAndNdCode(commonInfo, ndCode, mv.parentNvCode, codeFdName);
      if (!idMsg.success) {
        return idMsg;
      } else {
        mv.parentNvId = idMsg.value;
      }
    }
    const tabPage = {
      name: mv.name,
      title: mv.description,
      moduleCode: ConstDef.MODULE_CODE_ENTRY,
      config: mv
    }
    result.value = tabPage;
    result.success = true;
    return result;
  }

  async getEntryConfig(mdCode, mvCode) {
    const tagPageMsg = await this.getEntryConfigTabPage(mdCode, mvCode);
    if (tagPageMsg.success) {
      const config = tagPageMsg.value.config;
      return {
        success: true,
        config
      }
    } else return tagPageMsg;
  }

  async getFilterConfigTabPag(mdCode, mvCode) {
    const commonInfo = cacheManage.getDefaultCommonInfo();
    const mvMsg = await mdService.getMv(commonInfo, mdCode, mvCode);
    const result = { success: false, message: null }
    if (!mvMsg.success) {
      result.message = mvMsg.message;
      return result;
    }
    const mv = mvMsg.value;
    const tabPage = {
      name: mv.name,
      title: mv.description,
      moduleCode: ConstDef.MODULE_CODE_FILTER,
      config: { ...mv, action: { autoFilter: false } }
    }
    result.value = tabPage;
    result.success = true;
    return result;
  }

  async getFilterConfig(mdCode, mvCode) {
    const commonInfo = cacheManage.getDefaultCommonInfo();
    const mvMsg = await mdService.getMv(commonInfo, mdCode, mvCode);
    const config = { ...mvMsg.value, action: { autoFilter: false } }
    mvMsg.value = config;
    return mvMsg;
  }

  async getCompositeTabPage(mdCode, mvCode) {
    const commonInfo = cacheManage.getDefaultCommonInfo();
    const mvMsg = await mdService.getMv(commonInfo, mdCode, mvCode);
    const result = { success: false, message: null }
    if (!mvMsg.success) {
      result.message = mvMsg.message;
      return result;
    }
    const mv = mvMsg.value;
    const tabPages = [];
    const funcDetailMvs = mv.funcDetailNd;
    for (const funcDetailMv of funcDetailMvs) {
      let detailConfigMsg;
      if (funcDetailMv.moduleCode === ConstDef.MODULE_CODE_FILTER) {
        detailConfigMsg = await this.getFilterConfigTabPag(funcDetailMv.funcMdCode, funcDetailMv.funcMvCode);
      } else if (funcDetailMv.moduleCode === ConstDef.MODULE_CODE_ENTRY) {
        detailConfigMsg = await this.getEntryConfigTabPage(funcDetailMv.funcMdCode, funcDetailMv.funcMvCode);
      }
      if (detailConfigMsg.success) {
        tabPages.push(detailConfigMsg.value);
      } else {
        return detailConfigMsg;
      }
    }
    const tabPage = {
      name: mv.name,
      title: mv.description,
      moduleCode: ConstDef.MODULE_CODE_STEPS,
      config: mv
    }
    mv.tabPages = tabPages;
    result.value = tabPage;
    result.success = true;
    return result;
  }
}

const menuFunction = new MenuFunction();
export default menuFunction;
